﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DrDimsPrivateLib.MqLothub2018WithWPF
{
    using MVVMLight;
    using GalaSoft.MvvmLight.Messaging;
    using WPFUtil;
    using LibMQTT.ProtocalIMPL;

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        internal const string DesignSignal = "GlobalMessageForUI";
        internal const string UISingal = "UISingal";

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (sender, e) => {
                // 0.设置窗体居中
                WPFLambdaUtil.SetWPFWindowCenter(this);
                // 0.1 设置窗体的父类和Canvas---> Grid 大小一致
                LayoutSubViews();

                // 1.注册MVVMLight消息机
                Messenger.Default.Register<MqPayloadVariable>(this,DesignSignal, MVVMLightMsgMachine);
                Messenger.Default.Register<string>(this,UISingal, MVVMLightButtonPage);
                // 2.绑定主页上下文ViewModel视图模型
                DataContext = new TransportViewModel(this.Dispatcher);
            };

        }
        /// 0.1 设置窗体的父类和Canvas---> Grid 大小一致
        public void LayoutSubViews()
        {
            superView.Width = TopView.Width = this.Width;
            superView.Height = TopView.Height = this.Height;
            this.BottomView.SetValue(Canvas.LeftProperty, WPFLambdaUtil.ScreenSize.Width - BottomView.Width);
            this.BottomView.SetValue(Canvas.TopProperty, WPFLambdaUtil.ScreenSize.Height - BottomView.Height);
        }
        /// <summary>
        /// 为MVVMLight组件注册 消息处理机
        /// </summary>
        /// <param name="token"> token可以是MVVMLight组件传过来的任意对象 不一定是Mqtt字符串 </param>
        public void MVVMLightMsgMachine(object token)
        {
            string info = token.ToString();
            Dispatcher.Invoke(() => {
                Title = info;
                MsgDataGridBox.Background = new SolidColorBrush(WPFLambdaUtil.SolidRColor);
            });
        }
        public void MVVMLightButtonPage(object token)
        {
            string info = token.ToString();
            Title = info;
        }
    }
}
